<template>
  <el-card>
    <el-form :inline="true">
      <el-form-item label="一级分类">
        <el-select
          v-model="categoryStore.category1Id"
          placeholder="请选择"
          @change="handleCategory1Change()"
          clearable
          :disabled="scene === 1"
        >
          <el-option
            v-for="item in categoryStore.category1List"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          v-model="categoryStore.category2Id"
          placeholder="请选择"
          @change="handleCategory2Change()"
          clearable
          :disabled="scene === 1"
        >
          <el-option
            v-for="item in categoryStore.category2List"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          v-model="categoryStore.category3Id"
          clearable
          placeholder="请选择"
          :disabled="scene === 1"
        >
          <el-option
            v-for="item in categoryStore.category3List"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            clearable
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: 'Category',
}
</script>
<script lang="ts" setup>
import { onMounted } from 'vue'
//引入分类仓库
import useCategoryStore from '@/store/modules/category.ts'

defineProps({
  scene: {
    type: Number,
    default: 0,
  },
})
let categoryStore = useCategoryStore()

//组件挂载完毕
onMounted(() => {
  getCategory1()
})

const getCategory1 = () => {
  categoryStore.getCategory1List()
}
//一级分类改变时获取二级分类
const handleCategory1Change = () => {
  //一级分类改变时，二级分类和三级分类都要清空
  categoryStore.category2Id = ''
  categoryStore.category3Id = ''
  categoryStore.category2List = []
  categoryStore.category3List = []
  categoryStore.getCategory2List()
}
//二级分类改变时获取三级分类
const handleCategory2Change = () => {
  //三级分类都要先清空
  categoryStore.category3Id = ''
  categoryStore.category3List = []
  categoryStore.getCategory3List()
}
</script>

<style scoped lang="scss">
.el-form--inline {
  .el-form-item {
    .el-input,
    .el-cascader,
    .el-select,
    .el-autocomplete {
      width: 240px;
    }
  }
}
</style>
